export function addTouch(ele,page){
	var _this=this;
    var touch=ele;
    var SWIPE_DISTANCE = document.body.clientWidth/4;
    var point_start,
        point_end;
    //计算两点之间距离
    var getDist = function(p1 , p2){
        if(!p1 || !p2) return 0;
        return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    //获取touch的点(兼容mouse事件)
    var getTouchPos = function(e){
        var touches = e.touches;
        if(touches && touches[0]) {
            return { x : touches[0].clientX ,
                     y : touches[0].clientY };
        }
        return { x : e.clientX , y: e.clientY };
    }
    //获取swipe的方向
    var getSwipeDirection = function(p2,p1){
        var dx = p2.x - p1.x;
        var dy = -p2.y + p1.y;    
        var angle = Math.atan2(dy , dx) * 180 / Math.PI;

        if(angle < 45 && angle > -45) return false;
        if(angle >= 45 && angle < 135) return true;
        if(angle >= 135 || angle < -135) return false;
        if(angle >= -135 && angle <= -45) return true;

    }
    var scorllAction = function(dvalue,page){
        let scrollAct_state = sessionStorage.getItem("scrollAct")
        if (scrollAct_state == "true") {
            if(dvalue>0){
                if(page<7){
                    document.getElementById("touchpad").style.transform="translateY(-"+page*100+"%"+")"
                }else{
                    
                }          
            }else{
                document.getElementById("touchpad").style.transform="translateY(-"+(page-2)*100+"%"+")"
            }
        }
        
        
       
    }
    var startEvtHandler = function(e){
        var pos = getTouchPos(e);
        var touches = e.touches; 
        if( !touches || touches.length == 1 ){ //touches为空，代表鼠标点击
            point_start = getTouchPos(e);
            
        }
         // event.preventDefault();
    }
    var moveEvtHandler = function(e){
        point_end = getTouchPos(e);
        let scrollAct_state = sessionStorage.getItem("scrollAct")
        if(scrollAct_state == "true"){
            event.preventDefault();
        }
        
    }

    var endEvtHandler = function(e){ 
        if(getDist(point_start,point_end) > SWIPE_DISTANCE&&getSwipeDirection(point_start,point_end)){
           scorllAction(point_start.y-point_end.y,page);
        }
        point_start=0;
        point_end=0;
    }

    //判断是PC或者移动设备
    var startEvt, moveEvt, endEvt;
    if("ontouchstart" in window){
        startEvt="touchstart";
        moveEvt="touchmove";
        endEvt="touchend";
    }else{
        startEvt="mousedown";
        moveEvt="mousemove";
        endEvt="mouseup";            
    }
    touch.addEventListener(startEvt, startEvtHandler);
    touch.addEventListener(moveEvt, moveEvtHandler);
    touch.addEventListener(endEvt, endEvtHandler);
}